package com.example.demo.utils;

import org.springframework.format.Formatter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

@Component
public class DateFormatter implements Formatter<Date> {

    private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

    @SuppressWarnings("null")
    @Override
    public Date parse(String text, Locale locale) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(ISO_FORMAT, locale);
        return format.parse(text);
    }

    @SuppressWarnings("null")
    @Override
    public String print(Date object, Locale locale) {
        SimpleDateFormat format = new SimpleDateFormat(ISO_FORMAT, locale);
        return format.format(object);
    }
}
